﻿using System.Drawing;
using System.Windows.Forms;
using System;

namespace PocketGPW
{
    public class DataGridChangeTextBoxColumn : DataGridTextBoxColumn
    {
        private DataGrid _dg;
        private StringFormat _sf;
        private SolidBrush _foreBrushNegative = new SolidBrush(Color.Red);
        private SolidBrush _foreBrushPositive = new SolidBrush(Color.Green);

        public DataGridChangeTextBoxColumn(DataGrid dg)
        {
            _dg = dg;

            _sf = new StringFormat();
            _sf.Alignment = StringAlignment.Center;
            _sf.LineAlignment = StringAlignment.Center;
        }

        protected override void Paint(Graphics g, Rectangle bounds, CurrencyManager source, int rowNum, Brush backBrush, Brush foreBrush, bool alignToRight)
        {
            // napis            
            object value = base.PropertyDescriptor.GetValue(source.List[rowNum]);
            if (value != null && value is float)
            {
                // tło
                g.FillRectangle(backBrush, bounds); 
                // napis
                g.DrawString(value + "%", _dg.Font, (float)value < 0 ? _foreBrushNegative : _foreBrushPositive, bounds, _sf);
            }
            else
            {
                base.Paint(g, bounds, source, rowNum, backBrush, foreBrush, alignToRight);
            }
        }
    }
}
